﻿using System;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x02000210 RID: 528
public class SenseController
{
	// Token: 0x06000B25 RID: 2853 RVA: 0x0005E4CC File Offset: 0x0005C6CC
	public SenseController(EntityBase entity)
	{
		this.entity = entity;
		this.head = entity.transform;
		if (entity.animationManager != null)
		{
			this.head = entity.animationManager.unityAnimator.GetBoneTransform(HumanBodyBones.Head);
		}
	}

	// Token: 0x06000B26 RID: 2854 RVA: 0x0005E531 File Offset: 0x0005C731
	public bool CheckVisibility(EntityBase target)
	{
		return this.VisibilityTest(target);
	}

	// Token: 0x06000B27 RID: 2855 RVA: 0x0005E53C File Offset: 0x0005C73C
	private bool VisibilityTest(EntityBase target)
	{
		if (target == null)
		{
			return false;
		}
		Vector3 position = this.head.position;
		Vector3 vector = target.transform.position + Vector3.up * target.Height * 0.5f;
		float num = target.Height / this.entity.Height;
		float num2 = this.maxDistace * this.entity.Height * num;
		float num3 = this.fieldOfView * 0.5f;
		Debug.DrawRay(position, Quaternion.AngleAxis(-num3, this.head.up) * this.head.forward * num2, Color.yellow);
		Debug.DrawRay(position, Quaternion.AngleAxis(num3, this.head.up) * this.head.forward * num2, Color.yellow);
		Vector3 vector2 = (vector - position).normalized * num2;
		float num4 = Vector3.Distance(position, vector);
		if (num4 < num2)
		{
			float num5 = Vector3.Angle(this.head.forward, vector2);
			if (num5 < num3)
			{
				RaycastHit raycastHit;
				bool flag = Physics.Raycast(position, vector2, out raycastHit, num4, Layers.visibilityMask);
				if (!flag || raycastHit.transform == target.transform)
				{
					Debug.DrawLine(position, vector, Color.green);
					return true;
				}
				Debug.DrawLine(position, raycastHit.point, Color.red);
			}
		}
		return false;
	}

	// Token: 0x06000B28 RID: 2856 RVA: 0x0005E6C8 File Offset: 0x0005C8C8
	public List<EntityBase> GetEntitiesInRadius(float maxDistance)
	{
		return this.GetEntities(maxDistance, Layers.visibilityMask, false);
	}

	// Token: 0x06000B29 RID: 2857 RVA: 0x0005E6D7 File Offset: 0x0005C8D7
	public List<EntityBase> GetVisibleEntities(float maxDistance)
	{
		return this.GetEntities(maxDistance, Layers.visibilityMask, true);
	}

	// Token: 0x06000B2A RID: 2858 RVA: 0x0005E6E6 File Offset: 0x0005C8E6
	public List<EntityBase> GetVisibleMicros(float maxDistance)
	{
		return this.GetEntities(maxDistance, Layers.crushableMask, true);
	}

	// Token: 0x06000B2B RID: 2859 RVA: 0x0005E6F8 File Offset: 0x0005C8F8
	private List<EntityBase> GetEntities(float maxDistance, LayerMask mask, bool visible)
	{
		Vector3 position = this.entity.transform.position;
		float radius = this.entity.Height * this.maxDistace;
		List<EntityBase> list = new List<EntityBase>();
		Collider[] array = Physics.OverlapSphere(position, radius, mask);
		foreach (Collider collider in array)
		{
			EntityBase component = collider.gameObject.GetComponent<EntityBase>();
			if (component != null && !component.isDead && !component.locked)
			{
				list.Add(component);
			}
		}
		if (visible)
		{
			list = list.FindAll((EntityBase target) => this.VisibilityTest(target));
		}
		return list;
	}

	// Token: 0x06000B2C RID: 2860 RVA: 0x0005E7B8 File Offset: 0x0005C9B8
	public EntityBase GetRandomMicro(float distance)
	{
		List<EntityBase> visibleMicros = this.GetVisibleMicros(distance);
		if (visibleMicros.Count > 0)
		{
			return visibleMicros[UnityEngine.Random.Range(0, visibleMicros.Count)];
		}
		return null;
	}

	// Token: 0x04001045 RID: 4165
	private EntityBase entity;

	// Token: 0x04001046 RID: 4166
	private Transform head;

	// Token: 0x04001047 RID: 4167
	public float maxDistace = 200f;

	// Token: 0x04001048 RID: 4168
	public float fieldOfView = 100f;
}
